package com.dnzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IOperationDao;
import com.dnzx.model.OperationBO;
import com.dnzx.service.IOperationService;

@Service
public class OperationServiceImpl implements IOperationService {

    @Resource
    private IOperationDao operationDao;

    @Override
    public void save(OperationBO operationBO) throws Exception {
    	// 要先删除之前的配置，再保存新的配置
    	this.operationDao.deleteByCategoryId(operationBO.getCategoryId());
    	List<String> operationList = operationBO.getOperationList();
    	if (operationList != null && operationList.size() > 0) {
    		OperationBO operationBODb = null;
    		for (int i = 0; i < operationList.size(); i++) {
    			operationBODb = new OperationBO();
    			operationBODb.setCategoryId(operationBO.getCategoryId());
    			operationBODb.setCode(operationList.get(i));
    			operationBODb.setIconUrl(operationList.get(i));
    			this.operationDao.save(operationBODb);
    		}
    	}
    }

    @Override
    public void delete(Long id) throws Exception {
        this.operationDao.delete(id);
    }

    @Override
    public void update(OperationBO operationBO) throws Exception {
        this.operationDao.update(operationBO);
    }
    
    @Override
    public OperationBO get(Long id) {
        return this.operationDao.get(id);
    }
    
    @Override
	public OperationBO get(OperationBO operationBO) {
    	List<OperationBO> operationBOList = this.operationDao.queryList(operationBO);
    	if (operationBOList != null && operationBOList.size() > 0) {
    		return operationBOList.get(0);
    	}
		return null;
	}
    
    @Override
    public List<OperationBO> queryList(OperationBO operationBO) {
        return this.operationDao.queryList(operationBO);
    }

    @Override
    public PageResult<List<OperationBO>> queryPage(OperationBO operationBO, PageResult<List<OperationBO>> pageResult) {
    	int totalRows = this.operationDao.queryCount(operationBO);
    	pageResult.setTotalRows(totalRows);
    	pageResult.setCurrentPage(operationBO.getCurrentPage());
    	pageResult.setPageSize(operationBO.getPageSize());
    	List<OperationBO> records = this.operationDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), operationBO);
    	pageResult.setT(records);
    	return pageResult;
    }

}
